package com.define.ssh.util;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JsonUtil {

	private static final String JACKSON_OBJECT_MAPPER_BEAN_NAME = "jacksonObjectMapper";// jackson
																						// ObjectMapper
																						// Bean名称

	public static ObjectMapper getMapper() {
		return (ObjectMapper) SpringUtil
				.getBean(JACKSON_OBJECT_MAPPER_BEAN_NAME);
	}

	// 将对象转换为JSON字符串
	public static String toJson(Object object) {
		final ObjectMapper mapper = getMapper();
		try {
			return mapper.writeValueAsString(object);
		} catch (final Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 将JSON字符串转换为对象
	public static <T> T toObject(String json, Class<T> clazz) {
		final ObjectMapper mapper = getMapper();
		try {
			return mapper.readValue(json, clazz);
		} catch (final Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 将JSON字符串转换为对象
	@SuppressWarnings("unchecked")
	public static <T> T toObject(String json, TypeReference typeReference)
			throws JsonParseException, JsonMappingException, IOException {
		final ObjectMapper mapper = getMapper();
		return mapper.readValue(json, typeReference);
	}

	/**
	 * JSON转换为Map对象-返回集合的形式
	 */
	public static List<LinkedHashMap<String, Object>> readJson2Map(String json) {
		try {
			final ObjectMapper objectMapper = getMapper();
			final List<LinkedHashMap<String, Object>> list = objectMapper
					.readValue(json, List.class);
			/**
			 * System.out.println(list.size()); for (int i = 0; i < list.size();
			 * i++) { Map<String, Object> map = list.get(i); Set<String> set =
			 * map.keySet(); for (Iterator<String> it =
			 * set.iterator();it.hasNext();) { String key = it.next();
			 * System.out.println(key + ":" + map.get(key)); } }
			 **/
			return list;
		} catch (final JsonParseException e) {
			e.printStackTrace();
		} catch (final JsonMappingException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * JSON转换为Map对象-- 返回单挑模式
	 */
	public static Map<String, Object> readJson2Map2(String json) {
		try {
			final ObjectMapper objectMapper = new ObjectMapper();
			// ObjectMapper objectMapper = getMapper();
			final Map<String, Object> maps = objectMapper.readValue(json,
					Map.class);
			return maps;
		} catch (final JsonParseException e) {
			e.printStackTrace();
		} catch (final JsonMappingException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * JSON转换为数组对象
	 */
	public static Object[] readJson2Array(String json) {
		try {
			final ObjectMapper objectMapper = new ObjectMapper();
			final Object[] ojbectArray = (Object[]) objectMapper.readValue(
					json, Object.class);
			return ojbectArray;
		} catch (final JsonParseException e) {
			e.printStackTrace();
		} catch (final JsonMappingException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}